package com.wb.utils

import com.wb.component.Slf4j
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * 批站 工具
 *
 * @author 祁雪
 */
@Slf4j
class BiBiLiLiUtil {

    companion object {

        /**
         * 内容参数
         * context:
        page: 页码
        order: 排序 可不传
        keyword: 搜索词
        duration:
        tids_1:
        tids_2:
        from_source:
        // from_spmid:333.337
        platform:pc
        __refresh__:true 是否刷新搜索
        _extra:
        search_type:video 搜索类型
        highlight:1 是否高亮 更改无效
        single_column:0
         */
        val searchUrl = "https://api.bilibili.com/x/web-interface/search/type"

        /**
         * 搜索词
         */
        fun search(key: String, order: String): List<BiBiLiLiUtil.SearchData> {
            println("组装哔哩哔哩搜索参数 => $key")

            val param = hashMapOf<String, String>(
                "context" to "",
                "page" to "1",
                // 排序  click.最多点击 pubdate.最新发布 dm.最多弹幕  stow.最多收藏 空字符串.综合排序
                "order" to order,
                "keyword" to key,
                "duration" to "",
                "tids_1" to "",
                "tids_2" to "",
                "from_source" to "",
                // from_spmid:333.337
                "platform" to "",
                "__refresh__" to "true",
                "_extra" to "",
                "search_type" to "video",
                "highlight" to "",
                "single_column" to "0"
            )
            println("开始访问")
            val result = RestUtil.sendGet(searchUrl, param)

            println("开始解析")

            val gson = Gson()

            var map = gson.fromJson(
                result,
                object : TypeToken<HashMap<String, Any>>() {}.type
            ) as HashMap<String, Any>

            if (map.containsKey("data")) {
                map = gson.fromJson(
                    gson.toJson(map.get("data")),
                    object : TypeToken<HashMap<String, Any>>() {}.type
                ) as HashMap<String, Any>
                if (map.containsKey("result")) {
                    val list = gson.fromJson(
                        gson.toJson(map.get("result")),
                        object : TypeToken<List<SearchData>>() {}.type
                    ) as List<SearchData>
                    println("解析到数据共${list.size}条")
                    return list
                }
            }

            return arrayListOf()
        }

    }

    class SearchData {
        // 类型
        var type: String = ""

        // id
        var id: Long = 0

        // 作者
        var author: String = ""

        // mid
        var mid: Long = 0

        // typeid
        var typeid: Long = 0

        // 类型名称
        var typename: String = ""

        // 视频地址
        var arcurl: String = ""

        // 视频id
        var aid: Long = 0

        // bv号
        var bvid: String = ""

        // 标题
        var title: String = ""

        // 描述内容
        var description: String = ""

        // 缩略图地址
        var pic: String = ""

        // 标签
        var tag: String = ""

        // 播放数量
        var play: Long = 0

        override fun toString(): String {
            return "SearchData(type='$type', id=$id, author='$author', mid=$mid, typeid=$typeid, typename='$typename', arcurl='$arcurl', aid=$aid, bvid='$bvid', title='$title', description='$description', pic='$pic', tag='$tag', play='$play')"
        }

    }

}

fun main() {
    val list = BiBiLiLiUtil.search("萌爱moi", "")
    list.forEach { println(it) }
//    BiBiLiLiUtil.getCat(list.get(0))
}